
\subsubsection{Code generation for \textsf{\small throw}}

Generating the code for \textsf{\small throw} is quite simple since Java already has an instruction to raise exceptions : the \textsf{\small athrow} instruction. 
A reference to the exception that will be thrown needs to be pushed on top of the stack just before this instruction is executed.
Since in TOOL, the \textsf{\small throw} statement takes an \textsf{\small ExprTree} as a parameter, we need to generate the code that will evaluate this expression and push it on the stack.
Since \textsf{\small ExprTree} wasn't modified in the extension, the function to generate an expression evaluation code could be used for that purpose.
We didn't need to modify it to any extend.

The \textsf{\small athrow} instruction can then be safely generated.

\subsubsection{Code generation for simple \textsf{\small try/catch}}

The part was a little more tricky than the previous one, as it implied a little more complex interactions with CAFEBABE and more low-level details.
In particular, we had to add entries into a field of the class files : the exception table.
The exception table is sort of an interrupt table for Java : when an exception is raised, the JVM will sequentially search for an handler in the exception table.
If no handler if found, the exception is sent to the caller function, and so on.
If an handler is found, the program counter will be set to the address of the handler, and the execution will continue.

Exception table entries have four fields : 

\begin{itemize}

\item Two of these fields contains an offset (an offset for the beginning, one for the end) to tell the JVM for which portion of the code the handler is valid. 
For a regular \textsf{\small try/catch}, these offset correspond to the code inside the \textsf{\small try}.

\item One of these fields specify the address of the handler.

\item The last field contains an offset pointing in the constant pool, specifying which exception is handled in the corresponding \textsf{\small catch} block.

\end{itemize}

The entries are added using the \textsf{\small addHandler} method of CAFEBABE (see next section for more informations on this method).

The code generation for a typical \textsf{\small try/catch} statement is done like this : 

\begin{enumerate}

\item The code for the \textsf{\small try} statement is generated

\item A \textsf{\small goto} is generated, this will make the execution jumps to the rest of the code if no exception were raised in the \textsf{\small try}.

\item The handlers for the \textsf{\small catch} statements are then generated one after another. The same \textsf{\small goto} as in the previous step is generated  at the end of each handler.

\end{enumerate}

Of course, labels need to be generated for each handler, for the end and the start of the \textsf{\small try} and to mark the end of the handlers (used in the \textsf{\small gotos} statements).
The general structure for the bytecode of a \textsf{\small try/catch} is thus :

\begin{center}
%\begin{tabular}{c}

tryStartLabel \\

\textit{code for the try} \\

\textit{goto endlabel} \\
		
tryEndLabel \\

catch1Label \\

\textit{code for handler 1} \\

\textit{goto endLabel} \\

\textit{...} \\

catchNLabel \\

\textit{code for handler N} \\

\textit{goto endlabel} \\
	
endlabel \\
%\end{tabular}
\end{center}

The exception table will have the following entries : 

\begin{center}

\begin{tabular}{|c|c|c|c|}

\hline

tryStartLabel & tryEndLabel & exception of catch 1 & catch1Label \\

\hline

\multicolumn{4}{c}{...} \\

\hline

tryStartLabel & tryEndLabel & exception of catch N & catchNLabel \\

\hline

\end{tabular}

\end{center}

\subsubsection{Code generation for \textsf{\small finally}}

Code generation for \textsf{\small try/catch} with a \textsf{\small finally} statement is more complex : new instructions are to be used and special entries need to be added into the exception table.

The idea with \textsf{\small finally} is that its body is to be executed whatever happened inside the \textsf{\small try} or the \textsf{\small catch}(es). 
If an unhandled exception is raised inside of one these block, the body of \textsf{\small finally} must be executed, and the exception resent.
\textsf{\small Finally} blocks are very useful when, for example, files or sockets are to be closed regardless of the outcome of the program's execution.

A special value \textsf{\small any} is used in the exception table to match ``all possible exceptions''.
The idea is that if an exception which isn't handled in any \textsf{\small catch} block is raised, then it will be caught by the entry with the \textsf{\small any} value.

There are two ways the execution flow can exit a \textsf{\small try} body in TOOL :


\begin{itemize}

\item an exception is raised
\begin{itemize}

\item if the exception is handled, the corresponding handler is executed, followed by the \textsf{\small finally}.
 If an exception is raised inside the \textsf{\small catch}, the control is transfered to the \textsf{\small finally}, and the exception sent to the caller.

\item if not, the \textsf{\small finally} is directly executed and the exception sent to the caller

\end{itemize}

\item the execution flow naturally exits the \textsf{\small try}, the \textsf{\small finally} is executed right after

\end{itemize}

The problem is that once the \textsf{\small finally} has been executed, there are different possible outcomes : either an exception is sent or the execution continues.
This is why the instruction \textsf{\small jsr} (for Jump Sub Routine) needs to be used if we don't want the body of the \textsf{\small finally} to be duplicated. 
This instruction acts like the \textsf{\small call} instruction of x86 : it pushes the return address on the stack and sets the program counter to its argument.
It is used in conjonction with the \textsf{\small ret} instruction, which takes a register as argument. \textsf{\small Ret} sets the program counter to the value contained in the register passed as argument.

\medskip

Here is the code structure for a \textsf{\small try/catch/finally} : 

\begin{center}

%\begin{tabular}{c}

tryStartLbl \\

\textit{code for try} \\

\textit{goto endOfBlock} \\

tryEndLbl \\

catch1Lbl \\
	
\textit{code for catch 1} \\
	
\textit{goto endOfBlock} \\
	
\textit{...} \\
	
catchNLbl \\
	
\textit{code for catch N} \\
	
\textit{goto endOfBlock} \\
	
catchesEndLbl \\
	
finallyExceptionLbl \\
	
\textit{store exception in register n} \\
	
\textit{jsr finallyCodeLbl} \\
	
\textit{load exception from register n} \\
	
\textit{athrow} \\

finallyCodeLbl \\

\textit{store return address in register n} \\

\textit{code for finally} \\

\textit{ret n} \\

endOfBlock \\

\textit{jsr finallyCodeLbl} \\
%\end{tabular}

\end{center}

Here are the entries that will be found in the exception table : 

\begin{center}

\begin{tabular}{|c|c|c|c|}

\hline

tryStartLbl & tryEndLbl & exception of catch 1 & catch1Lbl \\

\hline

\multicolumn{4}{c}{...} \\

\hline

tryStartLbl & tryEndLbl & exception of catch N & catchNLbl \\

\hline

tryStartLbl & catchesEndLbl & \textsf{\small any} & finallyExceptionLbl \\

\hline

\end{tabular}

\end{center}

The only difference with the previous version of the exception table is the add of \textsf{\small any} entry.
The code portion ``protected'' by this handler ranges from the start of the \textsf{\small try} statements to the end of the \textsf{\small catch} statements.
This way, if any unexpected exception is raised in this interval, it will be caught by this handler.
This exception table is consistent with the different possibilities the control flows can quit a \textsf{\small try} : if an handled exception is raised in the \textsf{\small try}, the handler will be called, thanks to the first $n - 1$ entries of the exception table.
If the handler is entirely executed, the \textsf{\small goto} at the end of each handler will transfer the execution flow to the \textsf{\small finally} block.
If an exception is raised in an handler, it will be captured by the last entry of the exception table, and the \textsf{\small finally} will be executed via the \textsf{\small finallyExceptionLbl} label.
If the try is entirely executed, the \textsf{\small goto} statement at the end will transfer the execution flow to the \textsf{\small finally}. 

All the different cases are thus handled.
